package io.renren.data;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.RenrenApplication;
import io.renren.modules.avanotes.dao.VocabularyDao;
import io.renren.modules.avanotes.dao.WordVoiceDao;
import io.renren.modules.avanotes.entity.VocabularyEntity;
import io.renren.modules.avanotes.entity.WordVoiceEntity;
import io.renren.modules.avanotes.service.WordVoiceService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: xiaomi
 * @date: 2022/2/8
 * @description: 发音导入
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RenrenApplication.class)
@Slf4j
public class PronounceImport {

    @Autowired
    WordVoiceDao wvDao;

    @Autowired
    WordVoiceService wvService;

    @Autowired
    VocabularyDao vDao;


    String sourcePath = "E:\\File\\nginx-1.14.2\\html\\resource\\voice";

    /**
     * 导入原始数据（路径）
     */
    @Test
    public void importSourceData() {
        File file = new File(sourcePath);
        List<WordVoiceEntity> ll = new ArrayList<>();
        recordData(file, ll);
        //batch insert
        wvService.saveBatch(ll);
    }

    /**
     * 更新单词的音频路径
     * 剩下848 没有更新
     */
    @Test
    public void updatePronunciationRelativePath() {

        //1. 获取所有单词
        List<VocabularyEntity> list = vDao.selectList(new QueryWrapper<>());
        for (VocabularyEntity entity : list) {
            WordVoiceEntity wordVoiceEntity = wvDao.selectOne(new QueryWrapper<WordVoiceEntity>().eq(WordVoiceEntity.CN_LOWER_STRING, entity.getWord().toLowerCase()));
            if (wordVoiceEntity != null) {
                VocabularyEntity updator = new VocabularyEntity();
                updator.setId(entity.getId());
                updator.setPronunciationRelativePath(wordVoiceEntity.getRelativePath());
                log.info("准备更新：{}，{}", entity.getWord(), wordVoiceEntity.getRelativePath());
                vDao.updateById(updator);
            }
        }
    }

    /**
     * 传入的必为目录路径
     *
     * @param dir
     * @param ll
     */
    public void recordData(File dir, List<WordVoiceEntity> ll) {
        String[] list = dir.list();
        for (String s : list) {
            //判断子目录
            String childPath = Paths.get(dir.getPath(), s).toString();
            File file = new File(childPath);
            if (file.isDirectory()) {
                recordData(file, ll);
            } else if (childPath.endsWith(".wav")) {
                addEntity(s, childPath, ll);
            } else {
                log.info("留下没有处理的文件：{}", childPath);
            }

        }

    }

    private void addEntity(String s, String childPath, List<WordVoiceEntity> ll) {
        String str = s.replace(".wav", "");
        WordVoiceEntity entity = new WordVoiceEntity(str, str.toLowerCase(), childPath.replace(sourcePath, ""));
//        wvDao.insert(entity);
        //改为批量插入
        ll.add(entity);
    }
}
